package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.dongtai.PraiseDetailActivity;
import com.beiing.tianshuai.tianshuai.dongtai.util.BubbleSort;
import com.beiing.tianshuai.tianshuai.dongtai.view.DynamicDetailActivity;
import com.beiing.tianshuai.tianshuai.entity.DynamicBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicCommentAndReplyBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.mine.ui.NewPersonalIndexActivity;
import com.beiing.tianshuai.tianshuai.util.EmotionUtils;
import com.beiing.tianshuai.tianshuai.util.SpanStringUtils;
import com.beiing.tianshuai.tianshuai.util.TimeUtils;
import com.beiing.tianshuai.tianshuai.widget.ApproveListLayout;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
import com.netease.nim.uikit.common.util.log.LogUtil;
import com.qmuiteam.qmui.span.QMUITouchableSpan;
import com.qmuiteam.qmui.widget.textview.QMUISpanTouchFixTextView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Jinni on 2017/7/17 0017.
 * 显示动态的适配器，会将图片传输给nineGridView显示
 */

public class DynamicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    /**
     * // 普通动态
     */
    private static final int DYNAMIC_NORMAL = 1;
    /**
     * // 转发动态
     */
    private static final int DYNAMIC_FROM_OTHER = 0;
    private List<DynamicBean.DataBean.DynamicsBean> mList;
    private Context mContext;
    private OnItemClickListener mOnItemClickListener;
    private OnItemViewClickListener mOnItemViewClickListener;
    private OnCommentClickListener mOnCommentClickListener;
    private OnTextClickListener mOnTextClickListener;
    private ArrayList<ArrayList<DynamicCommentAndReplyBean>> arrayListArrayList;
    public static String dynamicId;
    public static String relayDynamicId;

    public DynamicAdapter(Context mContext, List<DynamicBean.DataBean.DynamicsBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
        arrayListArrayList = new ArrayList<>();
    }

    @Override
    public int getItemViewType(int position) {
        int result = 0;
        switch (mList.get(position).getTableType()) {
            case "1":
                result = DYNAMIC_NORMAL;
                break;
            case "0":
                result = DYNAMIC_FROM_OTHER;
                break;
            default:
                break;
        }
        return result;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case DYNAMIC_NORMAL:
                holder = new NormalHolder(View.inflate(mContext, R.layout.item_dynamic_normal, null));
                break;
            case DYNAMIC_FROM_OTHER:
                holder = new OtherHolder(View.inflate(mContext, R.layout.item_dynamic_from_other, null));
                break;
            default:
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        DynamicBean.DataBean.DynamicsBean bean = mList.get(position);
        if (holder instanceof NormalHolder) {
            ((NormalHolder) holder).setData(bean);
            ((NormalHolder) holder).setListener(bean);
        } else if (holder instanceof OtherHolder) {
            ((OtherHolder) holder).setData(bean);
            ((OtherHolder) holder).setListener(bean);
        }
    }

    private void checkTextViewLinesCount(final TextView content, final TextView viewAll) {
        content.post(new Runnable() {
            @Override
            public void run() {
                int lineCount = content.getLineCount();
                if (lineCount >= 5) {
                    viewAll.setVisibility(View.VISIBLE);
                } else {
                    viewAll.setVisibility(View.GONE);
                }
            }
        });
    }

    public void update() {
        arrayListArrayList.clear();
        notifyDataSetChanged();
    }

    // 删除数据
    public void removeData(int position) {
        mList.remove(position);
        notifyDataSetChanged();
    }

    public void deleteComment() {

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (mList != null) {
            return mList.size();
        } else {
            return 0;
        }
    }

    @Override
    public void onClick(View view) {
        if (mOnItemViewClickListener != null) {
            mOnItemViewClickListener.onItemViewClick(view, (int) view.getTag());
        }

        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(view, (int) view.getTag());
        }
    }

    class NormalHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ll_user_avatar)
        LinearLayout mLlUserAvatar;
        @BindView(R.id.user_avatar)
        CircleImageView mUserAvatar;
        @BindView(R.id.user_name)
        TextView mUserName;
        @BindView(R.id.contact_name)
        TextView mContactName;
        @BindView(R.id.dynamic_more)
        ImageView mMoreMenu;
        @BindView(R.id.dynamic_content)
        TextView mDynamicContent;
        @BindView(R.id.nine_grid_view)
        NineGridView mNineGridView;
        @BindView(R.id.dynamic_publish_time)
        TextView mPublishTime;
        @BindView(R.id.tv_delete)
        TextView mDynamicDelete;
        @BindView(R.id.num_like)
        TextView mNumLike;
        @BindView(R.id.num_comment)
        TextView mNumComment;
        @BindView(R.id.num_reprint)
        TextView mNumReprint;
        @BindView(R.id.tv_add_one)
        TextView mTvAddOne;
        @BindView(R.id.dynamic_view_all)
        TextView mDynamicViewAll;
        @BindView(R.id.normal_item_recycler)
        RecyclerView commentRecycler;
        @BindView(R.id.tv_check_comment)
        TextView tvCheckComment;
        @BindView(R.id.avatars_container)
        LinearLayout avatarsContainer;
        @BindView(R.id.approve_list)
        ApproveListLayout approveList;
        @BindView(R.id.thing_num)
        TextView thingNum;
        @BindView(R.id.split_praise_comment)
        View splitPraiseAndComment;

        private DynamicListComment_RVAdapter commentAdapter;

        NormalHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        private void setData(final DynamicBean.DataBean.DynamicsBean bean) {

            String avatar = bean.getPhoto();                // 作者头像
            String name = bean.getName();                   // 作者昵称
            String unit = bean.getUnit();                   // 作者的学校
            String content = bean.getContent();             // 动态文字内容
            String createTime = bean.getCreateTime();       // 动态发布时间
            String relay = bean.getRelay();                 // 动态转发数量
            String comment = bean.getComment();             // 动态评论数量
            String thing = bean.getThing();                 // 动态点赞数量
            int isThing = bean.getIsThing();                // 当前登录用户是否点赞

            // 动态图片列表
            List<DynamicBean.DataBean.DynamicsBean.PicBean> picBeanList = bean.getPic();
            // 动态点赞用户列表
            List<DynamicBean.DataBean.DynamicsBean.ThingIdBean> thingIdBeans = bean.getThingId();
            // 动态评论列表
            List<DynamicBean.DataBean.DynamicsBean.CommentTextBean> commentTextBeans = bean.getCommentText();
            // 动态评论的回复列表
            List<DynamicBean.DataBean.DynamicsBean.ReplyTextBean> replyText = bean.getReplyText();

            // 设置头像
            setAvatar(avatar, mUserAvatar);

            // 设置昵称
            mUserName.setText(TextUtils.isEmpty(name) ? "" : name);

            // 设置学校
            mContactName.setText(TextUtils.isEmpty(unit) ? "" : unit);

            // 设置发表时间
            mPublishTime.setText(TimeUtils.getTime(createTime));

            // 若是自己发布的动态，显示删除按键
            if (bean.getUid().equals(UserInfoBean.getUid(mContext))) {
                mDynamicDelete.setVisibility(View.VISIBLE);
            } else {
                mDynamicDelete.setVisibility(View.GONE);
            }

            // 设置转发数量
            mNumReprint.setText(relay);

            // 设置评论数量
            mNumComment.setText(comment);

            // 设置点赞数量
            mNumLike.setText(thing);

            // 设置点赞图标
            Drawable red_praise = mContext.getResources().getDrawable(R.drawable.ic_dynamic_praise_red);
            Drawable gray_praise = mContext.getResources().getDrawable(R.drawable.ic_dynamic_praise);
            if (isThing == 1) {
                mNumLike.setCompoundDrawablesWithIntrinsicBounds(red_praise, null, null, null);
            } else {
                mNumLike.setCompoundDrawablesWithIntrinsicBounds(gray_praise, null, null, null);
            }

            // 设置内容
            if (TextUtils.isEmpty(content)) {
                // 如果动态的文字内容为null，隐藏文字视图和查看全文提示
                mDynamicContent.setVisibility(View.GONE);
                mDynamicViewAll.setVisibility(View.GONE);
            } else {
                // 动态文字内容不为null，显示文字视图，根据文字所占据的lines判断是否显示查看全文提示
                mDynamicContent.setVisibility(View.VISIBLE);
                try {
                    String contentDecode = URLDecoder.decode(bean.getContent(), "UTF-8");
                    mDynamicContent.setText(SpanStringUtils.getEmotionContent(mContext, mDynamicContent, contentDecode));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                checkTextViewLinesCount(mDynamicContent, mDynamicViewAll);
            }

            // 设置动态的图片 动态图片列表
            if (picBeanList.isEmpty()) {
                mNineGridView.setVisibility(View.GONE);
            } else {
                mNineGridView.setVisibility(View.VISIBLE);
                ArrayList<ImageInfo> imageInfos = new ArrayList<>();
                for (DynamicBean.DataBean.DynamicsBean.PicBean picBean : picBeanList) {
                    String url = HttpRequestConstant.DYNAMIC_PIC_HEAD + picBean.getPicname();
                    ImageInfo imageInfo = new ImageInfo();
                    //小图URL
                    imageInfo.setThumbnailUrl(url);
                    //大图URL
                    imageInfo.setBigImageUrl(url);
                    int height=Integer.parseInt(picBean.getHeight());
                    imageInfo.setImageViewHeight(height);
                    imageInfo.setImageHeight(height);
                    int width=Integer.parseInt(picBean.getWidth());
                    int ratio=height/width;
                    //如果图片长度大于5000比例大于1就会是长图
                    if ( height> 5000&&ratio>2) {
                        LogUtil.i("图片长度", "DynamicAdapter" + picBean.getPicname() +
                                height+"  "+ratio);
                    }
                    imageInfo.setImageViewWidth(width);
                    imageInfo.setImageWidth(width);
                    imageInfos.add(imageInfo);
                }
                mNineGridView.setAdapter(new NineGridViewClickAdapter(mContext, imageInfos));
            }

            //点赞与评论之间分割线
            if (!thingIdBeans.isEmpty() && !commentTextBeans.isEmpty()) {
                splitPraiseAndComment.setVisibility(View.VISIBLE);
            } else {
                splitPraiseAndComment.setVisibility(View.GONE);
            }

            //点赞头像部分
            if (thingIdBeans.isEmpty()) {
                avatarsContainer.setVisibility(View.GONE);
            } else {
                avatarsContainer.setVisibility(View.VISIBLE);
                List<String> approveUrlList = new ArrayList<>();
                for (DynamicBean.DataBean.DynamicsBean.ThingIdBean thingIdBean : thingIdBeans) {
                    approveUrlList.add(thingIdBean.getPhoto());
                }
                approveList.updateApproveList(approveUrlList);
                thingNum.setText(thingIdBeans.size() > 13 ? "+" + String.valueOf(thingIdBeans.size()) + "人赞" : String.valueOf(thingIdBeans.size() + "人赞"));
            }

            // 设置评论和回复的数据
            if (!commentTextBeans.isEmpty()) {
                commentRecycler.setVisibility(View.VISIBLE);
                ArrayList<DynamicCommentAndReplyBean> commentAndReplyBeans = new ArrayList<>();
                for (DynamicBean.DataBean.DynamicsBean.CommentTextBean commentTextBean : commentTextBeans) {
                    DynamicCommentAndReplyBean CARBean = new DynamicCommentAndReplyBean();
                    CARBean.setDid(commentTextBean.getAid());
                    CARBean.setId(commentTextBean.getId());
                    CARBean.setUid(commentTextBean.getUid());
                    CARBean.setTo_uid(commentTextBean.getToUid());
                    CARBean.setContent(commentTextBean.getContent());
                    CARBean.setCreate_time(commentTextBean.getCreateTime());
                    CARBean.setTable_type(commentTextBean.getTableType());
                    CARBean.setF_name(commentTextBean.getName());
                    CARBean.setT_name("");
                    commentAndReplyBeans.add(CARBean);
                }
                if (replyText != null && !replyText.isEmpty()) {
                    for (DynamicBean.DataBean.DynamicsBean.ReplyTextBean replyTextBean : replyText) {
                        DynamicCommentAndReplyBean CARBean = new DynamicCommentAndReplyBean();
                        CARBean.setDid(replyTextBean.getDid());
                        CARBean.setId(replyTextBean.getId());
                        CARBean.setUid(replyTextBean.getForm_uid());
                        CARBean.setTo_uid(replyTextBean.getTo_uid());
                        CARBean.setContent(replyTextBean.getContent());
                        CARBean.setCreate_time(replyTextBean.getCreate_time());
                        CARBean.setTable_type(replyTextBean.getTable_type());
                        CARBean.setF_name(replyTextBean.getfName());
                        CARBean.setT_name(replyTextBean.gettName());
                        commentAndReplyBeans.add(CARBean);
                    }
                }
                // 判断评论和回复的总数，大于6条的时候显示查看“更多评论”提示，否则隐藏。
                if (commentAndReplyBeans.size() > 6) {
                    tvCheckComment.setVisibility(View.VISIBLE);
                } else {
                    tvCheckComment.setVisibility(View.GONE);
                }
                LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
                commentRecycler.setLayoutManager(manager);
                if (commentAndReplyBeans.size() > 6) {
                    commentAdapter = new DynamicListComment_RVAdapter(mContext, BubbleSort.sortList(commentAndReplyBeans).subList(0, 5));
                } else {
                    commentAdapter = new DynamicListComment_RVAdapter(mContext, BubbleSort.sortList(commentAndReplyBeans));
                }
                commentRecycler.setAdapter(commentAdapter);
                commentAdapter.setOnItemClickListener(new DynamicListComment_RVAdapter.OnItemClickListener() {

                    @Override
                    public void onItemClick(View view, int comment_position, DynamicCommentAndReplyBean commentAndReplyBean) {
                        if (mOnCommentClickListener != null) {
                            mOnCommentClickListener.onItemClick(view, getAdapterPosition(), comment_position, commentAndReplyBean, bean);
                        }
                    }
                });

                commentAdapter.setOnNameClickListener(new DynamicListComment_RVAdapter.OnNameClickListener() {

                    @Override
                    public void onNameClick(View view, int comment_position, int text_position, DynamicCommentAndReplyBean commentAndReplyBean) {
                        if (mOnTextClickListener != null) {
                            mOnTextClickListener.onTextClick(view, getAdapterPosition(), comment_position, text_position, commentAndReplyBean);
                        }
                    }
                });
            } else {
                commentRecycler.setVisibility(View.GONE);
            }
        }

        private void setListener(final DynamicBean.DataBean.DynamicsBean bean) {

            mUserAvatar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnUserAvatarClickListener != null) {
                        String uid = bean.getUid();
                        mOnUserAvatarClickListener.onUserAvatarClick(uid);
                    }
                }
            });

            // 删除
            mDynamicDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnDeleteClickListener != null) {
                        mOnDeleteClickListener.onDeleteClick(bean, getAdapterPosition());
                    }
                }
            });

            // 转发
            mNumReprint.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnReprintClickListener != null) {
                        mOnReprintClickListener.onReprintClick(bean, getAdapterPosition());
                    }
                }
            });

            // 评论点击事件
            mNumComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemCommentClickListener != null) {
                        mOnItemCommentClickListener.onItemCommentClickListener(bean, getAdapterPosition());
                    }
                }
            });

            // 点赞点击事件
            mNumLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnLikeClickListener != null) {
                        mOnLikeClickListener.onLikeClick(view, getAdapterPosition(), bean,
                                mTvAddOne);
                    }
                }
            });

            // 更多菜单点击事件
            mMoreMenu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemViewClickListener.onMoreClick(view, getAdapterPosition(), bean);
                }
            });

            // 条目点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemViewClickListener != null) {
                        mOnItemViewClickListener.onItemClick(view, getAdapterPosition(), bean);
                    }
                }
            });

            approveList.setOnHeaderClickListener(new ApproveListLayout.OnHeaderClickListener() {
                @Override
                public void onHeaderClick(View view, int position) {
                    Intent intent = new Intent(mContext, NewPersonalIndexActivity.class);
                    intent.putExtra("uid", bean.getThingId().get(12 - position).getId());
                    mContext.startActivity(intent);
                }
            });

        }

        private void setAvatar(String avatar, CircleImageView imageView) {
            RequestOptions requestOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.drawable.default_avatar)
                    .centerCrop()
                    .override(100, 100);
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(avatar) ?
                            R.drawable.default_avatar :
                            HttpRequestConstant.USER_PIC_HEAD + avatar)
                    .apply(requestOptions)
                    .into(imageView);
        }
    }

    class OtherHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ll_user_avatar)
        LinearLayout mLlUserAvatar;
        @BindView(R.id.user_avatar)
        CircleImageView mUserAvatar;
        @BindView(R.id.user_name)
        TextView mUserName;
        @BindView(R.id.contact_name)
        TextView mContactName;
        @BindView(R.id.dynamic_more)
        ImageView mMoreMenu;
        @BindView(R.id.dynamic_reprint_content)
        TextView mDynamicReprintContent;
        @BindView(R.id.dynamic_original_username)
        TextView mDynamicOriginalName;
        @BindView(R.id.dynamic_original_content)
        QMUISpanTouchFixTextView mDynamicOriginalContent;
        @BindView(R.id.nine_grid_view)
        NineGridView mNineGridView;
        @BindView(R.id.dynamic_reprint_time)
        TextView mReprintTime;
        @BindView(R.id.tv_delete)
        TextView mDynamicDelete;
        @BindView(R.id.num_like)
        TextView mNumLike;
        @BindView(R.id.num_comment)
        TextView mNumComment;
        @BindView(R.id.num_reprint)
        TextView mNumReprint;
        @BindView(R.id.tv_add_one)
        TextView mTvAddOne;
        @BindView(R.id.dynamic_reprint_view_all)
        TextView mTvReprintViewAll;
        @BindView(R.id.dynamic_original_view_all)
        TextView mTvOriginalViewAll;
        @BindView(R.id.relay_item_recycler)
        RecyclerView commentRecycler;
        @BindView(R.id.tv_check_comment)
        TextView tvCheckComment;
        @BindView(R.id.avatars_container)
        LinearLayout avatarsContainer;
        @BindView(R.id.approve_list)
        ApproveListLayout approveList;
        @BindView(R.id.thing_num)
        TextView thingNum;
        @BindView(R.id.split_praise_comment)
        View splitPraiseAndComment;
        @BindView(R.id.ll_content)
        LinearLayout llContent;
        @BindView(R.id.tv_not_exist)
        TextView tvNotExist;

        private DynamicListComment_RVAdapter commentAdapter;

        OtherHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        private void setData(final DynamicBean.DataBean.DynamicsBean bean) {

            String rName = bean.getRName();                 // 转发者昵称
            String rPhoto = bean.getRPhoto();               // 转发者头像
            String runit = bean.getRunit();                 // 转发者的学校
            String rContent = bean.getRContent();           // 转发文字内容
            String name = bean.getName();                   // 源动态作者昵称
            String content = bean.getContent();             // 源动态文字内容
            String rCreateTime = bean.getRCreateTime();     // 转发的时间
            String thing = bean.getThing();                 // 点赞数量
            String comment = bean.getComment();             // 评论数量
            String relay = bean.getRelay();                 // 转发数量
            int isThing = bean.getIsThing();                // 当前登录用户是否点赞

            // 点赞人的信息
            List<DynamicBean.DataBean.DynamicsBean.ThingIdBean> thingId = bean.getThingId();
            // 源动态图片内容
            List<DynamicBean.DataBean.DynamicsBean.PicBean> pic = bean.getPic();
            // 动态的评论
            List<DynamicBean.DataBean.DynamicsBean.CommentTextBean> commentText = bean.getCommentText();
            // 动态的回复
            List<DynamicBean.DataBean.DynamicsBean.ReplyTextBean> replyText = bean.getReplyText();

            // 设置头像
            setAvatar(rPhoto, mUserAvatar);

            // 设置昵称
            mUserName.setText(TextUtils.isEmpty(rName) ? "" : rName);

            // 设置学校
            mContactName.setText(TextUtils.isEmpty(runit) ? "" : runit);

            // 设置发表时间
            mReprintTime.setText(TimeUtils.getTime(rCreateTime));

            // 若是自己发布的转发，显示删除按钮
            if (bean.getFormUid().equals(UserInfoBean.getUid(mContext))) {
                mDynamicDelete.setVisibility(View.VISIBLE);
            } else {
                mDynamicDelete.setVisibility(View.GONE);
            }

            // 设置转发数量
            mNumReprint.setText(relay);

            // 设置评论数量
            mNumComment.setText(comment);

            // 设置点赞数量
            mNumLike.setText(thing);

            // 设置点赞图标
            Drawable red_praise = mContext.getResources().getDrawable(R.drawable.ic_dynamic_praise_red);
            Drawable gray_praise = mContext.getResources().getDrawable(R.drawable.ic_dynamic_praise);
            if (isThing == 1) {
                mNumLike.setCompoundDrawablesWithIntrinsicBounds(red_praise, null, null, null);
            } else {
                mNumLike.setCompoundDrawablesWithIntrinsicBounds(gray_praise, null, null, null);
            }

            // 原动态已被删除 转发
            if (bean.getId() == null) {
                llContent.setVisibility(View.GONE);
                tvNotExist.setVisibility(View.VISIBLE);
            } else {
                //进行转发动态配置
                llContent.setVisibility(View.VISIBLE);
                tvNotExist.setVisibility(View.GONE);

                // 设置转发的文字内容
                if (TextUtils.isEmpty(rContent)) {
                    // 如果动态的文字内容为null，隐藏文字视图和查看全文提示
                    mDynamicReprintContent.setVisibility(View.GONE);
                    mTvReprintViewAll.setVisibility(View.GONE);
                } else {
                    // 动态文字内容不为null，显示文字视图，根据文字所占据的lines判断是否显示查看全文提示
                    mDynamicReprintContent.setVisibility(View.VISIBLE);
                    try {
                        String contentDecode = URLDecoder.decode(rContent, "UTF-8");
                        mDynamicReprintContent.setText(SpanStringUtils.getEmotionContent(mContext, mDynamicReprintContent, contentDecode));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    checkTextViewLinesCount(mDynamicReprintContent, mTvReprintViewAll);
                }

                // 设置源动态的文字内容
                mDynamicOriginalName.setVisibility(View.GONE);
                String contentDecode = "null";
                if (TextUtils.isEmpty(content)) {
                    // 如果动态的文字内容为null，隐藏文字视图和查看全文提示
                    mTvOriginalViewAll.setVisibility(View.GONE);
                } else {
                    // 动态文字内容不为null，显示文字视图，根据文字所占据的lines判断是否显示查看全文提示
                    try {
                        contentDecode = URLDecoder.decode(content, "UTF-8");
                        mDynamicOriginalContent.setText(SpanStringUtils.getEmotionContent(mContext, mDynamicOriginalContent, contentDecode));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    checkTextViewLinesCount(mDynamicOriginalContent, mTvOriginalViewAll);
                }
                SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
                stringBuilder.append("@").append(name).append(": ").append(TextUtils.isEmpty(contentDecode) ? "" : contentDecode);

                //表情拼接
                Resources res = mContext.getResources();
                String regexEmotion = "\\[([\u4e00-\u9fa5\\w+\\.])+\\]";
                Pattern patternEmotion = Pattern.compile(regexEmotion);
                Matcher matcherEmotion = patternEmotion.matcher(stringBuilder);
                while (matcherEmotion.find()) {
                    // 获取匹配到的具体字符
                    String key = matcherEmotion.group();
                    // 匹配字符串的开始位置
                    int start = matcherEmotion.start();
                    // 利用表情名字获取到对应的图片
                    Integer imgRes = EmotionUtils.getImgByName(key);
                    // 压缩表情图片
                    int size = (int) mDynamicOriginalContent.getTextSize() * 13 / 10;
                    Bitmap scaleBitmap = null;
                    if (imgRes != -1) {
                        Bitmap bitmap = BitmapFactory.decodeResource(res, imgRes);
                        scaleBitmap = Bitmap.createScaledBitmap(bitmap, size, size, true);
                        ImageSpan span = new ImageSpan(mContext, scaleBitmap);
                        stringBuilder.setSpan(span, start, start + key.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }

                }

                stringBuilder.setSpan(new QMUITouchableSpan(
                        Color.parseColor("#67A0FF"),
                        Color.parseColor("#67A0FF"),
                        Color.parseColor("#00000000"),
                        Color.parseColor("#E0E0E0")) {
                    @Override
                    public void onSpanClick(View view) {
                        Intent intent = new Intent(mContext, NewPersonalIndexActivity.class);
                        intent.putExtra("uid", bean.getUid());
                        mContext.startActivity(intent);
                    }
                }, 0, 3 + name.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                stringBuilder.setSpan(new QMUITouchableSpan(
                        Color.parseColor("#2C2C2C"),
                        Color.parseColor("#2C2C2C"),
                        Color.parseColor("#00000000"),
                        Color.parseColor("#E0E0E0")) {
                    @Override
                    public void onSpanClick(View view) {
                        Intent intent = new Intent(mContext, DynamicDetailActivity.class);
                        intent.putExtra("dynamic_id", bean.getId());
                        mContext.startActivity(intent);
                    }
                }, 4 + name.length(), stringBuilder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                mDynamicOriginalContent.setMovementMethodDefault();
                mDynamicOriginalContent.setText(stringBuilder);


                // 设置动态的图片 源动态图片内容 转发
                if (pic.isEmpty()) {
                    mNineGridView.setVisibility(View.GONE);
                } else {
                    mNineGridView.setVisibility(View.VISIBLE);
                    ArrayList<ImageInfo> imageInfos = new ArrayList<>();
                    for (DynamicBean.DataBean.DynamicsBean.PicBean picBean : pic) {
                        String url = HttpRequestConstant.DYNAMIC_PIC_HEAD + picBean.getPicname();
                        ImageInfo imageInfo = new ImageInfo();
                        imageInfo.setThumbnailUrl(url);
                        imageInfo.setBigImageUrl(url);
                        imageInfo.setImageViewHeight(Integer.parseInt(picBean.getHeight()));
                        imageInfo.setImageViewWidth(Integer.parseInt(picBean.getWidth()));
                        imageInfo.setImageHeight(Integer.parseInt(picBean.getHeight()));
                        imageInfo.setImageWidth(Integer.parseInt(picBean.getWidth()));
                        imageInfos.add(imageInfo);
                    }
                    mNineGridView.setAdapter(new NineGridViewClickAdapter(mContext, imageInfos));
                }
            }

            //点赞与评论之间分割线
            if (!thingId.isEmpty() && !commentText.isEmpty()) {
                splitPraiseAndComment.setVisibility(View.VISIBLE);
            } else {
                splitPraiseAndComment.setVisibility(View.GONE);
            }

            //点赞头像部分
            if (thingId.isEmpty()) {
                avatarsContainer.setVisibility(View.GONE);
            } else {
                avatarsContainer.setVisibility(View.VISIBLE);
                List<String> approveUrlList = new ArrayList<>();
                for (DynamicBean.DataBean.DynamicsBean.ThingIdBean thingIdBean : thingId) {
                    approveUrlList.add(thingIdBean.getPhoto());
                }
                approveList.updateApproveList(approveUrlList);
                thingNum.setText(thingId.size() > 13 ? "+" + String.valueOf(thingId.size()) + "人赞" : String.valueOf(thingId.size() + "人赞"));
            }

            // 设置评论和回复的数据
            if (!commentText.isEmpty()) {
                commentRecycler.setVisibility(View.VISIBLE);
                ArrayList<DynamicCommentAndReplyBean> commentAndReplyBeans = new ArrayList<>();
                for (DynamicBean.DataBean.DynamicsBean.CommentTextBean commentTextBean : commentText) {
                    DynamicCommentAndReplyBean CARBean = new DynamicCommentAndReplyBean();
                    CARBean.setDid(commentTextBean.getAid());
                    CARBean.setId(commentTextBean.getId());
                    CARBean.setUid(commentTextBean.getUid());
                    CARBean.setTo_uid(commentTextBean.getToUid());
                    CARBean.setContent(commentTextBean.getContent());
                    CARBean.setCreate_time(commentTextBean.getCreateTime());
                    CARBean.setTable_type(commentTextBean.getTableType());
                    CARBean.setF_name(commentTextBean.getName());
                    CARBean.setT_name("");
                    commentAndReplyBeans.add(CARBean);
                }
                if (replyText != null && !replyText.isEmpty()) {
                    for (DynamicBean.DataBean.DynamicsBean.ReplyTextBean replyTextBean : replyText) {
                        DynamicCommentAndReplyBean CARBean = new DynamicCommentAndReplyBean();
                        CARBean.setDid(replyTextBean.getDid());
                        CARBean.setId(replyTextBean.getId());
                        CARBean.setUid(replyTextBean.getForm_uid());
                        CARBean.setTo_uid(replyTextBean.getTo_uid());
                        CARBean.setContent(replyTextBean.getContent());
                        CARBean.setCreate_time(replyTextBean.getCreate_time());
                        CARBean.setTable_type(replyTextBean.getTable_type());
                        CARBean.setF_name(replyTextBean.getfName());
                        CARBean.setT_name(replyTextBean.gettName());
                        commentAndReplyBeans.add(CARBean);
                    }
                }
                // 判断评论和回复的总数，大于6条的时候显示查看“更多评论”提示，否则隐藏。
                if (commentAndReplyBeans.size() > 6) {
                    tvCheckComment.setVisibility(View.VISIBLE);
                } else {
                    tvCheckComment.setVisibility(View.GONE);
                }
                LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
                commentRecycler.setLayoutManager(manager);
                if (commentAndReplyBeans.size() > 6) {
                    commentAdapter = new DynamicListComment_RVAdapter(mContext, BubbleSort.sortList(commentAndReplyBeans).subList(0, 5));
                } else {
                    commentAdapter = new DynamicListComment_RVAdapter(mContext, BubbleSort.sortList(commentAndReplyBeans));
                }
                commentRecycler.setAdapter(commentAdapter);
                commentAdapter.setOnItemClickListener(new DynamicListComment_RVAdapter.OnItemClickListener() {

                    @Override
                    public void onItemClick(View view, int comment_position, DynamicCommentAndReplyBean commentAndReplyBean) {
                        if (mOnCommentClickListener != null) {
                            mOnCommentClickListener.onItemClick(view, getAdapterPosition(), comment_position, commentAndReplyBean, bean);
                        }
                    }
                });

                commentAdapter.setOnNameClickListener(new DynamicListComment_RVAdapter.OnNameClickListener() {

                    @Override
                    public void onNameClick(View view, int comment_position, int text_position, DynamicCommentAndReplyBean commentAndReplyBean) {
                        if (mOnTextClickListener != null) {
                            mOnTextClickListener.onTextClick(view, getAdapterPosition(), comment_position, text_position, commentAndReplyBean);
                        }
                    }
                });
            } else {
                commentRecycler.setVisibility(View.GONE);
            }

        }

        private void setAvatar(String avatar, CircleImageView imageView) {
            RequestOptions requestOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.drawable.default_avatar)
                    .centerCrop()
                    .override(100, 100);
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(avatar) ?
                            R.drawable.default_avatar :
                            HttpRequestConstant.USER_PIC_HEAD + avatar)
                    .apply(requestOptions)
                    .into(imageView);
        }

        private void setListener(final DynamicBean.DataBean.DynamicsBean bean) {
            approveList.setOnHeaderClickListener(new ApproveListLayout.OnHeaderClickListener() {
                @Override
                public void onHeaderClick(View view, int position) {
                    Intent intent = new Intent(mContext, NewPersonalIndexActivity.class);
                    intent.putExtra("uid", bean.getThingId().get(12 - position).getId());
                    mContext.startActivity(intent);
                }
            });

            mUserAvatar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnUserAvatarClickListener != null) {
                        String uid = bean.getUid();
                        mOnUserAvatarClickListener.onUserAvatarClick(uid);
                    }
                }
            });

            mDynamicDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnDeleteClickListener != null) {
                        mOnDeleteClickListener.onDeleteClick(bean, getAdapterPosition());
                    }
                }
            });

            // 转发
            mNumReprint.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnReprintClickListener != null) {
                        mOnReprintClickListener.onReprintClick(bean, getAdapterPosition());
                    }
                }
            });

            // 评论点击事件
            mNumComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemCommentClickListener != null) {
                        mOnItemCommentClickListener.onItemCommentClickListener(bean, getAdapterPosition());
                    }
                }
            });

            // 点赞点击事件
            mNumLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnLikeClickListener != null) {
                        mOnLikeClickListener.onLikeClick(view, getAdapterPosition(), bean,
                                mTvAddOne);
                    }
                }
            });

            // 更多菜单点击事件
            mMoreMenu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemViewClickListener != null) {
                        mOnItemViewClickListener.onMoreClick(view, getAdapterPosition(), bean);
                    }
                }
            });

            // 转发文字点击事件
            mDynamicOriginalContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemViewClickListener != null) {
                        mOnItemViewClickListener.onItemClick(v, getAdapterPosition(), bean);
                    }
                }
            });

            // 条目点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemViewClickListener != null) {
                        mOnItemViewClickListener.onItemClick(view, getAdapterPosition(), bean);
                    }
                }
            });
        }
    }

    /**
     * 条目点击事件
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    /**
     * 条目子View点击事件
     */
    public interface OnItemViewClickListener {
        void onItemViewClick(View view, int position);

        void onMoreClick(View view, int position, DynamicBean.DataBean.DynamicsBean bean);

        void onItemClick(View view, int position, DynamicBean.DataBean.DynamicsBean bean);
    }

    public void setOnItemViewClickListener(OnItemViewClickListener listener) {
        this.mOnItemViewClickListener = listener;
    }

    //列表展示的评论的点击事件
    public interface OnCommentClickListener {
        void onItemClick(View view, int dynamicPosition, int commentPosition, DynamicCommentAndReplyBean commentAndReplyBean, DynamicBean.DataBean.DynamicsBean bean);
    }

    public void setOnCommentClickListener(OnCommentClickListener listener) {
        this.mOnCommentClickListener = listener;
    }

    public interface OnTextClickListener {
        void onTextClick(View view, int dynamicPosition, int commentPosition, int textPosition, DynamicCommentAndReplyBean commentAndReplyBean);
    }

    public void setOnTextClickListener(OnTextClickListener listener) {
        this.mOnTextClickListener = listener;
    }


    /**
     * 头像点击事件接口
     */
    public interface OnUserAvatarClickListener {
        void onUserAvatarClick(String uid);
    }

    private OnUserAvatarClickListener mOnUserAvatarClickListener;

    public void setOnUserAvatarClickListener(OnUserAvatarClickListener onUserAvatarClickListener) {
        mOnUserAvatarClickListener = onUserAvatarClickListener;
    }

    /**
     * 点赞事件接口
     */
    public interface OnLikeClickListener {
        void onLikeClick(View view, int position, DynamicBean.DataBean.DynamicsBean bean, TextView addOneTv);
    }

    private OnLikeClickListener mOnLikeClickListener;

    public void setOnLikeClickListener(OnLikeClickListener onLikeClickListener) {
        mOnLikeClickListener = onLikeClickListener;
    }

    /**
     * 条目评论按钮点击事件接口
     */
    public interface OnItemCommentClickListener {
        void onItemCommentClickListener(DynamicBean.DataBean.DynamicsBean bean, int position);
    }

    private OnItemCommentClickListener mOnItemCommentClickListener;

    public void setOnItemCommentClickListener(OnItemCommentClickListener onItemCommentClickListener) {
        mOnItemCommentClickListener = onItemCommentClickListener;
    }

    /**
     * 条目转发按钮点击事件接口
     */
    public interface OnReprintClickListener {
        void onReprintClick(DynamicBean.DataBean.DynamicsBean bean, int position);
    }

    private OnReprintClickListener mOnReprintClickListener;

    public void setOnReprintClickListener(OnReprintClickListener onReprintClickListener) {
        mOnReprintClickListener = onReprintClickListener;
    }

    public interface OnDeleteClickListener {
        void onDeleteClick(DynamicBean.DataBean.DynamicsBean bean, int position);
    }

    private OnDeleteClickListener mOnDeleteClickListener;

    public void setOnDeleteClickListener(OnDeleteClickListener listener) {
        mOnDeleteClickListener = listener;
    }
}
